#include <c8051f330.h>                 // SFR declarations
#include <stdio.h>

extern unsigned char code Version[];
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F33x
//-----------------------------------------------------------------------------
sfr16 TMR2RL   = 0xca;                 // Timer2 reload value
sfr16 TMR2     = 0xcc;                 // Timer2 counter

#define  ILEN  8                      			// size of serial receiving buffer       
unsigned char  istart;                			// receiving buffer start index          
unsigned char  iend;                  			// receiving buffer end index            
//idata    char  inbuf[ILEN];           			// storage for receiving buffer          
idata    char  BufOfInt[ILEN];
idata    char  BufOfSMBCN[ILEN];
idata    char  BufOfSMBDAT[ILEN];

unsigned int IntCounter;
unsigned char uSMB0CN;
unsigned char uSMB0DAT;
//bit bIntGeneral;

#include <ws_iic_port.h>
#include <ws_iic.h>
#include <DS18B20_TimerDelay_MulCh.h>

extern bit SMBus_BUSY;

void fnInitTimer2_2us (void)
{
   TMR2CN = 0x00;                      		// STOP Timer2; Clear TF2H and TF2L;
                                       		// disable low-byte interrupt; disable
                                       		// split mode; select internal timebase
   CKCON |= 0x10;                      		// Timer2 uses SYSCLK as its timebase

   TMR2RL  = -49;                  		// Init reload values	lei added 24.5*2=49
   TMR2    = TMR2RL;                   		// Init Timer2 with reload value
   ET2 = 0;                            	// disable Timer2 interrupts
//   TR2 = 1;                            		// start Timer2
}

void fnDelay2Xus(unsigned int nDelayTime)
{
   unsigned int count;

   TF2H = 0;                           // Clear Timer2 overflow flag
   TR2 = 1;                            // Start Timer2
   for (count = nDelayTime; count != 0; count--) {
      while (!TF2H);                   // wait for overflow
      TF2H = 0;                        // clear overflow indicator
   }
   TR2 = 0;                            // Stop Timer2
}

void Delay1s(void)
{
	unsigned int i;

	for(i=1000;i!=0;i--)
			fnDelay2Xus(500);
}

void main(void)
{
//	unsigned char uTemp;

	IntCounter = 0;

	PCA0MD &= ~0x40;                    	// WDTE = 0 (clear watchdog timer 
                                       		// enable)
	SYSCLK_Init();
	PORT_Init();
	UART0_Init();
	SMBus_Init();
	Timer3_Init();

	fnInitTimer2_2us();

	Enb_Interrupt();

//	printf("SMBus test Ver1.53\n");
//	printf(Version);

	SMBus_BUSY = 0;

	fnOw_Reset();
	fnWriteOneByte(0xcc); 			// Skip ROM
	fnWriteOneByte(0xbe);			// Read Scratch Pad
	fnReadLowByte();
	fnReadHighByte();

	fnOw_Reset();
	fnWriteOneByte(0xcc); 			//Skip ROM
	fnWriteOneByte(0x44); 			// Start Conversion

	while(1)
	{

//     	while(istart == iend);  
//		uTemp = BufOfInt[istart & (ILEN-1)];      	// transmit character         
//		uSMB0CN = BufOfSMBCN[istart & (ILEN-1)];      	// transmit character         
//		uSMB0DAT = BufOfSMBDAT[istart & (ILEN-1)];      	// transmit character         
//		istart++;

//		printf("\nIntCnt=%02bx; SMB0CN=%02bx; SMB0DAT=%02bx\n",uTemp,uSMB0CN,uSMB0DAT);

		fnOw_Reset();
		fnWriteOneByte(0xcc); 			// Skip ROM
		fnWriteOneByte(0xbe);			// Read Scratch Pad
  		fnReadLowByte();
		fnReadHighByte();

		fnOw_Reset();
 		fnWriteOneByte(0xcc); 			//Skip ROM
 		fnWriteOneByte(0x44); 			// Start Conversion

//  		printf("Temp0=0x%04x,%02.4f",Temperation[0].x,(float)(Temperation[0].x)/16);
//  		printf("  Temp1=0x%04x,%02.4f",Temperation[1].x,(float)(Temperation[1].x)/16);
//  		printf("  Temp2=0x%04x,%02.4f\n",Temperation[2].x,(float)(Temperation[2].x)/16);

//  		printf("Temp3=0x%04x,%02.4f",Temperation[3].x,(float)(Temperation[3].x)/16);
//  		printf("  Temp4=0x%04x,%02.4f",Temperation[4].x,(float)(Temperation[4].x)/16);
//  		printf("  Temp5=0x%04x,%02.4f\n",Temperation[5].x,(float)(Temperation[5].x)/16);

		Delay1s();
	}

	while(1);
}